package data_structure.test;

class CircleArrayQueueDemo {
    public static void main(String[] args) {
        CircleArrayQueue circleArrayQueue = new CircleArrayQueue(5);
        circleArrayQueue.addQueue(1);
        circleArrayQueue.addQueue(2);
        circleArrayQueue.addQueue(3);
        circleArrayQueue.addQueue(4);
        circleArrayQueue.addQueue(5);//队列已满
        circleArrayQueue.selectQueue();
        circleArrayQueue.getQueue();
        circleArrayQueue.getQueue();
        circleArrayQueue.getQueue();
        circleArrayQueue.getQueue();
        System.out.println();
        //circleArrayQueue.getQueue(); //runtimeException
    }
}

class CircleArrayQueue{
    private int maxSize;
    private int front;
    private int rear;
    private int[] arr;

    //初始化模拟队列
    public CircleArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[this.maxSize];
        front = 0;
        rear = 0;
    }

    //添加数据
    public void addQueue(int n){
        if (isFull()) {
            System.out.println("队列已满...");
            return;
        }
        arr[rear] = n;
        rear = (rear+1)%maxSize;
    }

    //出队操作
    public int getQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列为空...");
        }
        int value = arr[front];
        front = (front+1) % maxSize;
        return value;
    }

    //遍历数据
    public void selectQueue(){
        if (isEmpty()){
            System.out.println("数组为空...");
            return;
        }
        for (int i = front; i < front+size(); i++) {
            System.out.print(arr[i%maxSize]);
        }
    }

    //判断真正可以调用的数据 数量
    public int size(){
        return (rear-front+maxSize)%maxSize;
    }

    //队列判空
    public boolean isEmpty(){
        return rear == front;
    }

    //队列是否满
    public boolean isFull(){
        return (rear+1)%maxSize == front;
    }
}
